/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cognitivetouch.colorSpan;

import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLineWidth;

/**
 *
 * @author Xiaoxing
 */
public class ColorWheel {

    private static int centerX;
    private static int centerY;
    private static final float innerR = 50;
    private static final float outerR = 100;

    public static float[] getColor(float A) {
        /* A = arctan(Y, X)/PI   for normalized angle from X-Y plot        */
        /* get double R,G,B  R==1 at A==0, G==1 at A==2/3, B==1 at A==-2/3 */
        float AA, AAA; /* temporaries */

        float R, G, B;    /* red, green, blue intensities computer */

        R = 1.0f - Math.abs(A);
        AA = A - 2.0f / 3.0f;
        if (AA < -1.0) {
            AA = 2.0f + AA;
        }
        G = 1.0f - Math.abs(AA);
        AA = A + 2.0f / 3.0f;
        if (AA > 1.0) {
            AA = 2.0f - AA;
        }
        B = 1.0f - Math.abs(AA);

        /* boost low end and normalize (this brightens colors) */
        R = R + 0.2f;
        G = G + 0.2f;
        B = B + 0.2f;
        AAA = Math.max(R, G);
        AAA = Math.max(AAA, B);
        R = R / AAA;
        G = G / AAA;
        B = B / AAA;

        float[] rgb = {R, G, B};
        return rgb;
    }

    private static float remap(float preMap, boolean inner, boolean Y, int centerX, int centerY) {
        float coord;
        coord = preMap * (inner ? innerR : outerR);
        coord += (Y ? centerY : centerX);
        return coord;
    }

    public static float[] getlineXY(float A, int inputX, int inputY) {
        centerX = inputX;
        centerY = inputY;
        float coord = A * (float) Math.PI;
        /* compute line ends */
        float x1 = remap((float) Math.cos(coord), true, false, centerX, centerY);
        float y1 = remap((float) Math.sin(coord), true, true, centerX, centerY);
        float x2 = remap((float) Math.cos(coord), false, false, centerX, centerY);
        float y2 = remap((float) Math.sin(coord), false, true, centerX, centerY);

        float[] xys = {x1, y1, x2, y2};
        return xys;
    }

    public static void render(int centerX, int centerY) {

        float A;

        glLineWidth(3.0f);
        GL11.glBegin(GL11.GL_LINES);
        A = -1.0f;
        while (A < 1.0f) /* walk the angle in bams around the circle */ {

            float[] rgb = getColor(A);

            /* set cell colors */
            glColor3f(rgb[0], rgb[1], rgb[2]);

            float[] coords = getlineXY(A, centerX, centerY);

            GL11.glVertex2f(coords[0], coords[1]);
            GL11.glVertex2f(coords[2], coords[3]);

            A = A + 0.005f;  /* 200 lines */

        }
        glEnd();
    }

    public void drawColorLine(float A) {
        glLineWidth(50.0f);
        GL11.glBegin(GL11.GL_LINES);
        float[] rgb;
        if (A > -1 && A < 1) {
            rgb = getColor(A);
        } else {
            float[] temp = {0, 0, 0};
            rgb = temp;
        }

        /* set cell colors */
        glColor3f(rgb[0], rgb[1], rgb[2]);
        GL11.glVertex2f(100, 100);
        GL11.glVertex2f(100, 300);
        glEnd();
    }

    public int getCenterX() {
        return centerX;
    }

    public int getCenterY() {
        return centerY;
    }

    public float getInnerR() {
        return innerR;
    }

    public float getOuterR() {
        return outerR;
    }

}
